package com.deruan.hotcore.resource.model;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import com.deruan.hotcore.base.common.bean.ResourceBean;

public class ResourceModel implements IResourceModel
{
	
	private String modelId;
	
	private String contactId;

	private String sortString;
	
	private Vector<ResourceBean> resourceList = new Vector<ResourceBean>();
	
	private Integer nextLocalId = -1;

	public ResourceModel(String contactId)
	{
		this.modelId = contactId;
		this.contactId = contactId;
	}
	
    public String getModelId()
    {
    	return modelId;
    }
	
	public String getSortString(){
		return sortString;
	}
	
	public Iterator getResourceIterator()
	{
		return resourceList.iterator();
	}
	
	public Vector getResourceVector(){
		return resourceList;
	}

	public void setModelId(String id) {
		this.modelId = id;
	}

	public void setSortString(String sortString) {
		this.sortString = sortString;
	}
	
	public void addResource(ResourceBean res)
	{
		if (res == null)
			throw new IllegalArgumentException("res == null");
		resourceList.add(res);
		nextLocalId--;
	}
	
	public void addResourceList(Collection<ResourceBean> list)
	{
		resourceList.addAll(list);
		nextLocalId = nextLocalId - list.size();
	}

	public String getContactId() {
		return contactId;
	}

	public void setContactId(String contactId) {
		this.contactId = contactId;
	}
	
	public ResourceBean getResourceBeanById(Integer id)
	{
		Iterator<ResourceBean> iterator = getResourceIterator();
		ResourceBean bean = null;
		while (iterator.hasNext()) 
		{
			ResourceBean element = iterator.next();
			if(element.getId().intValue()==id.intValue())
			{
				bean = element;
				break;
			}
		}
		return bean;
	}
	
	public void updateResouceBean(ResourceBean orignBean, ResourceBean newBean)
	{
		resourceList.remove(orignBean);
		resourceList.add(newBean);
	}
	
	public void deleteResourceBean(Integer beanId)
	{
		Iterator<ResourceBean> iterator = resourceList.iterator();
		while (iterator.hasNext()) 
		{
			ResourceBean element = iterator.next();
			if(element.getId().intValue()==beanId.intValue())
			{
				resourceList.remove(element);
				break;
			}
		}
	}
	
	public Integer getNextLocalId()
	{
		return nextLocalId;
	}
	
}
